#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int c, t;
int a[N], num;
ll sum[N], removed;
deque<int> dq;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> c >> t;
  while (t--) {
    int op, x;
    cin >> op;
    if (op != 4) cin >> x;
    if (op == 1) {
      a[++num] = x;
      sum[num] = sum[num - 1] + a[num];
      while (dq.size() && a[dq.back()] <= a[num]) dq.pop_back();
      dq.push_back(num);
    } else if (op == 2) {
      removed += x;
      int id = upper_bound(sum + 1, sum + num + 1, removed) - sum;
      int x = id - 1;
      while (dq.size() && dq.front() <= x) dq.pop_front();
    } else if (op == 3) {
      ll y = removed + x;
      int id = lower_bound(sum + 1, sum + num + 1, y) - sum;
      ll d = sum[id] - y;
      ll res = a[id] - d;
      cout << res << endl;
    } else {
      cout << a[dq.front()] << endl;
    }
  }
  return 0;
}